package com.ls.data.service;

import com.alibaba.fastjson.JSONObject;
import com.ls.data.dao.InterfaceDao;
import com.ls.data.mapper.InterfaceMapper;
import com.ls.data.utils.ResultUtils;
import com.ls.interPojo.Air;
import com.ls.interPojo.Cook;
import com.ls.pojo.Inter;
import com.ls.pojo.InterThirdparty;
import com.ls.utils.Result;
import com.ls.utils.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

/**
 * @author 段成
 * @date 2020/1/9 - 9:55
 */
@Service
public class InterfaceService {

    @Autowired
    private InterfaceMapper interfaceMapper;


    public Inter getInterfaceByURL(String URL) {
        return interfaceMapper.getInterfaceByURL(URL);
    }







    @Autowired
    private InterfaceDao interfaceDao;

    @Autowired
    private HttpServletRequest request;

    public Result cook(String appkey, String menu) {
        List objects = new ArrayList<>();
        if (null == menu||menu.equals("")) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("erro", "菜单不能为空");
            objects.add(jsonObject);
            return new Result(true, StatusCode.OK,"菜单不能为空",objects,null);
        } else {
            //获取当前请求的地址
            StringBuffer requestURL = request.getRequestURL();
            //根据请求地址找到接口id
            Integer interfaceId = interfaceDao.findByUrl(requestURL.toString());
            if (interfaceId != null) {
                String key = interfaceDao.findByInterfaceId(interfaceId);
                //如果appkey和传入的key相同
                if (key.equals(appkey)) {
                    //去本地查找
                    List<Cook> byMenu = interfaceDao.findByMenu(menu);
                    //不为空的话直接返回
                    if (byMenu.size() != 0) {
                        return new Result(true, StatusCode.OK,"本地查询",objects,null);
                    } else {
                        //在本地数据库查不到的话去第三方查找
                        //先去数据库查找第三方的key和地址
                        InterThirdparty interThirdparty = interfaceDao.findBythird(interfaceId);
                        String url = interThirdparty.getThirdpartyUrl();
                        url += "?key=" + interThirdparty.getAppkey();
                        url += "&menu=" + menu;
                        JSONObject cooks = ResultUtils.Result(url);
                        Map result = (Map) cooks.get("result");
                        if (null == result) {
                            objects.add(cooks);
                            return new Result(true, StatusCode.OK,"错误信息",objects,null);
                        } else {
                            List data = (List) result.get("data");
                            Cook cook = new Cook();
                            List list = new ArrayList<>();
                            for (int i = 0; i < data.size(); i++) {
                                Map o = (Map) data.get(i);
                                int id1 = Integer.parseInt(o.get("id").toString());
                                cook.setId(id1);
                                String title = o.get("title").toString();
                                cook.setTitle(title);
                                String tags = o.get("tags").toString();
                                cook.setTags(tags);
                                String imtro = o.get("imtro").toString();
                                cook.setImtro(imtro);
                                String ingredients = o.get("ingredients").toString();
                                cook.setIngredients(ingredients);
                                String burden = o.get("burden").toString();
                                cook.setBurden(burden);
                                String steps = o.get("steps").toString();
                                cook.setSteps(steps);
                                list.add(cook);
                                interfaceDao.insertCook(cook);

                            }
                            return new Result(true, StatusCode.OK,"第三方查询",objects,null);

                        }
                    }

                }

                else {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("erro", "您的key输入有误");
                    objects.add(jsonObject);
                    return new Result(true, StatusCode.OK,"您的key输入有误",objects,null);
                }
            }else{
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("erro", "您的地址输入有误");
                objects.add(jsonObject);
                return new Result(true, StatusCode.OK,"您的地址输入有误",objects,null);
            }
        }
    }


    public Result air(String appkey, String city) {
        List objects = new ArrayList<>();
        if (null == city || city.equals("")) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("erro", "城市不能为空");
            objects.add(jsonObject);
            return new Result(true, StatusCode.OK,"城市不能为空",objects,null);
        } else {
            //获取当前请求的地址
            StringBuffer requestURL = request.getRequestURL();
            //
            Integer interfaceId = interfaceDao.findByUrl(requestURL.toString());

            if (interfaceId != null) {
                String key = interfaceDao.findByInterfaceId(interfaceId);
                //如果appkey和传入的key相同
                if (key.equals(appkey)) {
                    //去本地查找
                    Air bycity = interfaceDao.findBycity(city);
                    //不为空的话直接返回
                    if (bycity != null) {
                        objects.add(bycity);
                        return new Result(true, StatusCode.OK,"本地查询",objects,null);
                    } else {
                        //在本地数据库查不到的话去第三方查找
                        //先去数据库查找第三方的key和地址
                        InterThirdparty interThirdparty = interfaceDao.findBythird(interfaceId);
                        String url = interThirdparty.getThirdpartyUrl();
                        url += "?key=" + interThirdparty.getAppkey();
                        url += "&city=" + city;
                        JSONObject air = ResultUtils.Result(url);

                        List result = (List) air.get("result");
                        if (result.size() == 0) {
                            objects.add(air);
                            return new Result(true, StatusCode.OK,"错误信息",objects,null);
                        } else {
                            Air air1 = new Air();
                            air1.setCity(city);
                            Map o = (Map) result.get(0);
                            String citynow = o.get("citynow").toString();
                            air1.setCitynow(citynow);
                            String lastTwoWeeks = o.get("lastTwoWeeks").toString();
                            air1.setLastTwoWeeks(lastTwoWeeks);
                            String lastMoniData = o.get("lastMoniData").toString();
                            air1.setLastMoniData(lastMoniData);
                            interfaceDao.insertAir(air1);
                            objects.add(air1);

                            return new Result(true, StatusCode.OK,"第三方查询",objects,null);

                        }

                    }

                } else {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("erro", "您的key输入有误");
                    objects.add(jsonObject);
                    return new Result(true, StatusCode.OK,"您的key输入有误",objects,null);
                }
            } else {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("erro", "您的地址输入有误");
                objects.add(jsonObject);
                return new Result(true, StatusCode.OK,"您的地址输入有误",objects,null);
            }

        }
    }
}
